package com.cgx.service.impl;


import com.cgx.dao.UserDao;
import com.cgx.dao.UserInfoDao;
import com.cgx.entity.User.User;
import com.cgx.entity.User.UserInfo;
import com.cgx.service.UserService;
import com.cgx.utils.JwtUtils;
import com.cgx.utils.UUIDUtils;
import com.cgx.vo.base.BaseIdListVo;
import com.cgx.vo.user.UserInfoVo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.*;

/**
 * @author chenguanxin
 * @date 2021-05-09 22:26
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserInfoDao userInfoDao;

    @Override
    public void register(Map<String, Object> data) {
        Assert.notNull(data, "请求不能为空");
        Assert.hasText((String) data.get("phone"), "手机号码不能为空");
        Assert.hasText((String) data.get("loginName"), "登录名不能为空");
        Assert.hasText((String) data.get("password"), "密码不能为空");
        String phone = String.valueOf(data.get("phone"));
        String loginName = String.valueOf(data.get("loginName"));
        String password = String.valueOf(data.get("password"));
        List<User> userList = userDao.findAllByLoginNameAndIsDelete(loginName, 0);
        Assert.isTrue(CollectionUtils.isEmpty(userList), "该登录名已存在");
        List<User> phoneUserList = userDao.findAllByPhoneAndIsDelete(phone, 0);
        Assert.isTrue(CollectionUtils.isEmpty(phoneUserList), "该手机号码已存在");
        String userId = UUIDUtils.getUUID();
        User user = getUser(userId);
        user.setPhone(phone);
        user.setLoginName(loginName);
        user.setPassword(password);
        userDao.save(user);
        UserInfo userInfo = getUserInfo(userId);
        userInfo.setUserName(loginName);
        userInfo.setAvatarUrl("");
        userInfoDao.insert(userInfo);
    }

    private User getUser(String userId) {
        User user = new User();
        user.setUserId(userId);
        user.setCreateBy(userId);
        user.setCreateTime(new Date());
        user.setUpdateBy(userId);
        user.setUpdateTime(new Date());
        user.setIsDelete(0);
        return user;
    }

    @Override
    public Object login(Map<String, Object> data) {
        Assert.notNull(data, "请求不能为空");
        Assert.hasText((String) data.get("loginName"), "用户名不能为空");
        Assert.hasText((String) data.get("password"), "密码不能为空");
        String loginName = String.valueOf(data.get("loginName"));
        String password = String.valueOf(data.get("password"));
        boolean keepLogin = false;
        if (data.get("keepLogin") != null) {
            keepLogin = (boolean) data.get("keepLogin");
        }
        List<User> loginNameList = userDao.findAllByLoginNameAndIsDelete(loginName, 0);
        List<User> phoneList = userDao.findAllByPhoneAndIsDelete(loginName, 0);
        List<User> userList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(loginNameList)) {
            userList.addAll(loginNameList);
        }
        if (CollectionUtils.isNotEmpty(phoneList)) {
            userList.addAll(phoneList);
        }
        for (User user : userList) {
            if (password.equals(user.getPassword())) {
                Map<String, String> result = new HashMap<>();
                String userId = user.getUserId();
                long keepLoginTime = 24 * 60 * 60 * 1000;
                if (keepLogin) {
                    keepLoginTime = keepLoginTime * 5;
                }
                String token = JwtUtils.createJwt(userId, "", keepLoginTime);
                result.put("userId", userId);
                result.put("token", token);
                return result;
            }
        }
        throw new IllegalArgumentException("用户名或密码错误");
    }

    @Override
    public Object info(Map<String, Object> data) {
        Assert.notNull(data, "请求不能为空");
        Assert.hasText((String) data.get("userId"), "ID不能为空");
        Map<String, Object> result = new HashMap<>();
        String userId = String.valueOf(data.get("userId"));
        List<User> userList = userDao.findAllByUserIdAndIsDelete(userId, 0);
        if (CollectionUtils.isNotEmpty(userList)) {
            result.put("loginName", userList.get(0).getLoginName());
        }
        List<UserInfo> userInfoList = userInfoDao.findAllByUserIdAndIsDelete(userId, 0);
        if (CollectionUtils.isNotEmpty(userInfoList)) {
            result.put("userName", userInfoList.get(0).getUserName());
            result.put("avatarUrl", userInfoList.get(0).getAvatarUrl());
        }
        return result;
    }

    @Override
    public List<User> getUserListByIdList(List<String> idList) {
        return userDao.findAllByUserIdInAndIsDelete(idList, 0);
    }

    @Override
    public List<UserInfoVo> getUserInfo(List<String> userIdList) {
        Assert.notEmpty(userIdList, "userIdList不能为空");
        List<UserInfoVo> userInfoVoList = new ArrayList<>();
        List<User> userList = userDao.findAllByUserIdInAndIsDelete(userIdList, 0);
        List<UserInfo> userInfoList = userInfoDao.findAllByUserIdInAndIsDelete(userIdList, 0);
        Map<String, User> userMap = new HashMap<>();
        Map<String, UserInfo> userInfoMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(userList)) {
            userList.forEach(user -> userMap.put(user.getUserId(), user));
        }
        if (CollectionUtils.isNotEmpty(userInfoList)) {
            userInfoList.forEach(userInfo -> userInfoMap.put(userInfo.getUserId(), userInfo));
        }
        for (String userId : userIdList) {
            if (userMap.containsKey(userId)) {
                UserInfoVo userInfoVo = new UserInfoVo();
                userInfoVo.setUser(userMap.get(userId));
                if (userInfoMap.containsKey(userId)) {
                    userInfoVo.setUserInfo(userInfoMap.get(userId));
                }
                userInfoVoList.add(userInfoVo);
            }
        }
        return userInfoVoList;
    }

    @Override
    public Map<String, Object> updateUserInfo(Map<String, Object> data) {
        Assert.notNull(data, "请求不能为空");
        Assert.notNull(data.get("userId"), "ID不能为空");
        String userId = data.get("userId").toString();
        Object userName = data.get("userName");
        Object avatarUrl = data.get("avatarUrl");
        if (userName != null || avatarUrl != null) {
            List<UserInfo> userInfoList = userInfoDao.findAllByUserIdInAndIsDelete(Collections.singletonList(userId), 0);
            if (CollectionUtils.isNotEmpty(userInfoList)) {
                UserInfo userInfo = userInfoList.get(0);
                if (userName != null) {
                    userInfo.setUserName(userName.toString());
                }
                if (avatarUrl != null) {
                    userInfo.setAvatarUrl(avatarUrl.toString());
                }
                userInfo.setUpdateBy(userId);
                userInfo.setUpdateTime(new Date());
                userInfoDao.save(userInfo);
            }
        }
        return null;
    }

    private UserInfo getUserInfo(String userId) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userId);
        userInfo.setCreateBy(userId);
        userInfo.setCreateTime(new Date());
        userInfo.setUpdateBy(userId);
        userInfo.setUpdateTime(new Date());
        userInfo.setIsDelete(0);
        return userInfo;
    }
}
